"""
Phase 2: Safe Rates & Term Structure
=====================================
Tests Kopecky-Taylor prediction: aging → lower safe rates + wider/unchanged term premium.
"""

import sys
from pathlib import Path

import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/asset_returns")
MULTILATERAL_DIR = PROJECT_DIR.parent / "multilateral"
sys.path.insert(0, str(MULTILATERAL_DIR / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]

CONTROLS = ['rgdp_growth', 'inflation', 'fiscal_bal_gdp', 'kaopen', 'nfa_gdp_lag']


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label, feature_names=None):
    """Run PanelGLS and return results dict."""
    cols = [dep_var] + regressors
    available = [c for c in cols if c in df.columns]
    missing = set(cols) - set(available)
    if missing:
        print(f"  [{label}] Missing columns: {missing}")
        regressors = [r for r in regressors if r in df.columns]
        if dep_var not in df.columns:
            print(f"  [{label}] Dependent variable {dep_var} missing — skipping")
            return None

    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None

    names = feature_names or regressors
    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)

    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, "
          f"R²={gls.r_squared:.4f}, rho={gls.rho:.3f}")

    results = {
        'label': label,
        'dep_var': dep_var,
        'n_obs': gls.n_obs,
        'n_countries': gls.n_countries,
        'r_squared': gls.r_squared,
        'rho': gls.rho,
    }
    for i, name in enumerate(names):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<25} {gls.beta[i]:>8.4f} ({gls.se[i]:.4f}) {sig}")

    return results


def main():
    print("=" * 70)
    print("PHASE 2: Safe Rates & Term Structure")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "asset_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df)} obs")

    all_results = []
    demo_vars = ['Z_1', 'Z_2', 'Z_3']
    controls = [c for c in CONTROLS if c in df.columns]

    # ── Model 1: real_bond_10y = Z + controls ──
    r = run_model(df, 'real_bond_10y', demo_vars + controls,
                  "M1: Z → real 10y bond", demo_vars + controls)
    if r: all_results.append(r)

    # ── Model 2: real_short_3m = Z + controls ──
    r = run_model(df, 'real_short_3m', demo_vars + controls,
                  "M2: Z → real 3m rate", demo_vars + controls)
    if r: all_results.append(r)

    # ── Model 3: term_spread = Z + controls ──
    r = run_model(df, 'term_spread', demo_vars + controls,
                  "M3: Z → term spread", demo_vars + controls)
    if r: all_results.append(r)

    # ── Model 4: old_dep + youth_dep separately ──
    age_vars = ['old_dep', 'youth_dep']
    age_controls = [c for c in controls if c in df.columns]
    r = run_model(df, 'real_bond_10y', age_vars + age_controls,
                  "M4: age ratios → real 10y", age_vars + age_controls)
    if r: all_results.append(r)

    # ── Model 5: Z × KAOPEN ──
    interaction_vars = ['Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen']
    int_available = [v for v in interaction_vars if v in df.columns]
    if int_available:
        r = run_model(df, 'real_bond_10y', demo_vars + controls + int_available,
                      "M5: Z×KAOPEN → real 10y", demo_vars + controls + int_available)
        if r: all_results.append(r)

    # ── Model 6: OECD subsample ──
    oecd = df[df['iso3'].isin(OECD_38)].copy()
    r = run_model(oecd, 'real_bond_10y', demo_vars + controls,
                  "M6: OECD Z → real 10y", demo_vars + controls)
    if r: all_results.append(r)

    r = run_model(oecd, 'term_spread', demo_vars + controls,
                  "M6b: OECD Z → term spread", demo_vars + controls)
    if r: all_results.append(r)

    # ── Model 7: 5-year lag ──
    df_lag = df.copy()
    for zv in demo_vars:
        df_lag[f'{zv}_lag5'] = df_lag.groupby('iso3')[zv].shift(5)
    lag_vars = [f'{zv}_lag5' for zv in demo_vars]
    r = run_model(df_lag, 'real_bond_10y', lag_vars + controls,
                  "M7: Z_lag5 → real 10y", lag_vars + controls)
    if r: all_results.append(r)

    # ── Model 8: First-differenced ──
    df_diff = df.sort_values(['iso3', 'year']).copy()
    for var in ['real_bond_10y'] + demo_vars + controls:
        if var in df_diff.columns:
            df_diff[f'd_{var}'] = df_diff.groupby('iso3')[var].diff()
    d_dep = 'd_real_bond_10y'
    d_regs = [f'd_{v}' for v in demo_vars + controls if f'd_{v}' in df_diff.columns]
    r = run_model(df_diff, d_dep, d_regs, "M8: ΔZ → Δreal 10y", d_regs)
    if r: all_results.append(r)

    # ── Build results table ──
    print("\n\nBuilding results table ...")
    build_table(all_results)

    print("\n" + "=" * 70)
    print("Phase 2 complete.")
    print("=" * 70)


def build_table(all_results):
    """Save markdown results table."""
    if not all_results:
        print("  No results to tabulate.")
        return

    # Key coefficients to report
    key_vars = ['Z_1', 'Z_2', 'Z_3', 'old_dep', 'youth_dep',
                'Z_1_x_kaopen', 'Z_2_x_kaopen', 'Z_3_x_kaopen',
                'Z_1_lag5', 'Z_2_lag5', 'Z_3_lag5',
                'd_Z_1', 'd_Z_2', 'd_Z_3']

    md = ["# Safe Rates & Term Structure Results\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in all_results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")

    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in all_results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")

    md.append(f"\n*Controls: {', '.join(CONTROLS)}*")
    md.append("*PanelGLS with AR(1) correction, no fixed effects*")

    out_path = TABLES_DIR / "safe_rates.md"
    out_path.write_text('\n'.join(md))
    print(f"  Saved: {out_path}")


if __name__ == "__main__":
    main()
